package org.chen.controller;

import org.chen.common.dto.CategoryTreeDTO;
import org.chen.common.vo.ResultVO;
import org.chen.entity.Category;
import org.chen.request.CategoryRequest;
import org.chen.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    
    @GetMapping("/tree")
    public ResultVO<List<CategoryTreeDTO>> getCategoryTree() {
        List<CategoryTreeDTO> tree = categoryService.getCategoryTree();
        return ResultVO.success("获取分类树成功", tree);
    }
    
    @GetMapping("/sub/{parentId}")
    public ResultVO<List<Category>> getSubCategories(@PathVariable Long parentId) {
        List<Category> categories = categoryService.getSubCategories(parentId);
        return ResultVO.success("获取子分类成功", categories);
    }
    
    @GetMapping("/{id}")
    public ResultVO<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        if (category != null) {
            return ResultVO.success("获取分类成功", category);
        } else {
            return ResultVO.fail("分类不存在");
        }
    }
    
    @PostMapping("/batch")
    public ResultVO<List<Category>> getCategoriesByIds(@RequestBody List<Long> ids) {
        List<Category> categories = categoryService.getCategoriesByIds(ids);
        return ResultVO.success("批量获取分类成功", categories);
    }
    
    @PostMapping("/add")
    public ResultVO<Category> addCategory(
            @RequestBody CategoryRequest categoryRequest) {
        
        try {
            Category category = categoryService.addCategory(categoryRequest.getName(), categoryRequest.getParentId(), categoryRequest.getLevel());
            return ResultVO.success("添加分类成功", category);
        } catch (Exception e) {
            return ResultVO.fail("添加分类失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/update")
    public ResultVO<Boolean> updateCategory(
            @RequestParam("id") Long id,
            @RequestParam("name") String name) {
        
        try {
            boolean success = categoryService.updateCategory(id, name);
            if (success) {
                return ResultVO.success("更新分类成功", true);
            } else {
                return ResultVO.fail("更新分类失败，分类不存在");
            }
        } catch (Exception e) {
            return ResultVO.fail("更新分类失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/delete/{id}")
    public ResultVO<Boolean> deleteCategory(@PathVariable Long id) {
        try {
            boolean success = categoryService.deleteCategory(id);
            if (success) {
                return ResultVO.success("删除分类成功", true);
            } else {
                return ResultVO.fail("删除分类失败，分类不存在");
            }
        } catch (Exception e) {
            return ResultVO.fail("删除分类失败：" + e.getMessage());
        }
    }
}